package machineLearning.featurecalculator;

import java.sql.SQLException;
import java.util.List;

import rainbownlp.core.Artifact;
import rainbownlp.core.FeatureValuePair;
import rainbownlp.core.FeatureValuePair.FeatureName;
import rainbownlp.i2b2.sharedtask2012.ClinicalEvent;
import rainbownlp.i2b2.sharedtask2012.LinkExampleBuilder;
import rainbownlp.i2b2.sharedtask2012.TimexPhrase;
import rainbownlp.i2b2.sharedtask2012.featurecalculator.EventEvent.EventEventNormalizedDependencyFeatures;
import rainbownlp.machineLearning.IFeatureCalculator;
import rainbownlp.machineLearning.MLExample;
import rainbownlp.machineLearning.MLExampleFeature;
import rainbownlp.util.StringUtil;
import transientTables.TermsTFIDF;

public class SentenceSyntaxFeatures implements IFeatureCalculator {
	public static void main(String[] args) throws Exception
	{
		List<MLExample> trainExamples = 
			MLExample.getAllExamples(true);
		int count =0;
		
		for(MLExample example_to_process: trainExamples)
		{
			SentenceSyntaxFeatures sf =  new SentenceSyntaxFeatures();
			
			
			sf.calculateFeatures(example_to_process);
			count++;
			System.out.println("************* "+count+"/"+trainExamples.size());			
		}	
		count = 0;
		List<MLExample> testExamples = 
			MLExample.getAllExamples(false);

		for(MLExample example_to_process: testExamples)
		{
			SentenceSyntaxFeatures sf =  new SentenceSyntaxFeatures();
			
			
			sf.calculateFeatures(example_to_process);
			count++;
			System.out.println("************* "+count+"/"+testExamples.size());

		}	
		
	}
	@Override
	public void calculateFeatures(MLExample exampleToProcess) throws SQLException {
		Artifact sentence = exampleToProcess.getRelatedArtifact();
		
		for (Artifact token: sentence.getChildsArtifact())
		{
			String normalized= StringUtil.getTermByTermWordnet(token.getContent());
			Double weight_in_sentence = weightInSentence(sentence, normalized);
			FeatureValuePair tfidf = FeatureValuePair.getInstance(FeatureName.SentenceTFIDF, 
					token.getContent(),weight_in_sentence.toString());
			MLExampleFeature.setFeatureExample(exampleToProcess, tfidf);
		}
		
		
	}
	private Double weightInSentence(Artifact sent, String word) throws SQLException {
		
		return TermsTFIDF.getTermTFIDF(sent.getAssociatedFilePath(), word);
	}
}
